package com.hoshiicloud.market.controller.activities;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.market.entity.activities.Slideshow;
import com.hoshiicloud.market.entity.activities.param.SlideshowAddParam;
import com.hoshiicloud.market.entity.activities.param.SlideshowQueryParam;
import com.hoshiicloud.market.entity.activities.param.SlideshowUpdateParam;
import com.hoshiicloud.market.service.activities.SlideshowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:campchen
 * @Date:Created in 下午 05:07 2019/8/26
 * @PACKAGENAME com.hoshiicloud.market.controller.activities
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/market/slideshow")
@Api(value = "扩展SPU", tags = {"扩展SPU"})
public class SlideshowController {

    @Autowired
    private SlideshowService slideshowService;

    @ApiOperation(value = "分页查询slideshow(轮播图)")
    @PostMapping("/query/page")
    public Response<Page<Slideshow>> page(@RequestBody SlideshowQueryParam param) {
        try {
            log.info("分页查询slideshow(轮播图)");
            QueryWrapper<Slideshow> queryWrapper = new QueryWrapper<Slideshow>();
            queryWrapper.lambda().eq(null != param.getCompanyId(), Slideshow::getCompanyId, param.getCompanyId())
                    .eq(null != param.getPlatformId(), Slideshow::getPlatformId, param.getPlatformId())
                    .eq(null != param.getStoreId(), Slideshow::getStoreId, param.getStoreId())
                    .like(StringUtils.isNotBlank(param.getName()), Slideshow::getName, param.getName())
                    .eq(StringUtils.isNotBlank(param.getImageUrl()), Slideshow::getImageUrl, param.getImageUrl())
                    .eq(null != param.getJumpType(), Slideshow::getJumpType, param.getJumpType())
                    .eq(StringUtils.isNotBlank(param.getJumpUrl()), Slideshow::getJumpUrl, param.getJumpUrl())
                    .eq(param.getStatus() != null, Slideshow::getStatus, param.getStatus())
                    .eq(Slideshow::getDeleted,0)
                    .orderByDesc(Slideshow::getCreatedTime);

            Page<Slideshow> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(slideshowService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询扩展SPU异常", e);
            return Response.failed("分页查询扩展SPU异常");
        }
    }

    /**
     * 查询banner图片
     * @param param 查询参数
     * @return Response<List<Banner>>
     */
    @ApiOperation(value = "查询banner图片")
    @PostMapping("/list")
    public Response<List<Slideshow>> list(@RequestBody Slideshow param) {
        try {
            log.info("查询banner图片");
            QueryWrapper<Slideshow> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(param.getCompanyId() != null,Slideshow::getCompanyId,param.getCompanyId())
                    .eq(param.getPlatformId() != null, Slideshow::getPlatformId, param.getPlatformId())
                    .eq(param.getStoreId() != null, Slideshow::getStoreId, param.getStoreId())
                    .eq(param.getName() != null, Slideshow::getName, param.getName())
                    .eq(Slideshow::getStatus,1)
                    .eq(Slideshow::getDeleted,0)
                    .orderByDesc(Slideshow::getCreatedTime);
            return Response.data(slideshowService.list(queryWrapper,param.toString()));
        } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("查询banner图片异常", e);
            return Response.failed("查询banner图片异常"+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺轮播图")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Slideshow> queryById(@PathVariable("id") Long id) {
        return Response.data(slideshowService.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增店铺轮播图信息")
    public Response<Boolean> save(@RequestBody SlideshowAddParam param) {
        // Bean数据转换
        Slideshow entity;
        try {
            entity = BeanUtils.covertType(param, Slideshow.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        boolean save = slideshowService.save(entity);
        return Response.data(save);
    }

    @PutMapping("/updateById")
    @ApiOperation(value = "更新店铺轮播图信息")
    public Response<Boolean> updateById(@RequestBody SlideshowUpdateParam param) {
        // Bean数据转换
        Slideshow entity;
        try {
            entity = BeanUtils.covertType(param, Slideshow.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(slideshowService.updateById(entity));
    }

    @DeleteMapping("/deleted/{id}")
    @ApiOperation(value = "根据Id删除店铺轮播图片信息")
    public Response<Boolean> deletedById(@PathVariable("id") Long id) {
        return Response.data(slideshowService.deleteById(id));
    }

}
